/**
 * Created by billy on 2021/1/19.
 */
var _cmdHash = {
	log,
	help,
}

function log($action, options = {})
{
	var server_path = g.path.join(__dirname, "../../../script-log.js");
	if ($action == "start")
	{
		require("child_process").exec("pm2 start " + server_path);
		trace("log server started. http://localhost:50000");
	}
	else if ($action == "stop")
	{
		require("child_process").exec("pm2 delete script-log");
		trace("log server stoped");
	}
}

function help($argv)
{
	var fileName = $argv.name;
	if (g.fs.existsSync(_bin(fileName)))
	{

	}
	else
	{

	}

	var usage = g.fs.readFileSync(g.path.join(__dirname, "./usage.txt")).toString();
	trace(usage);
}

function has($cmd, $type = "cmd")
{
	if ($type == "cmd")
	{
		return !!_cmdHash[$cmd];
	}
}

function start(cmd, ...arg)
{
	var func = _cmdHash[cmd];
	func.apply(func, arg);
}

function checkAndStart($argv)
{
	if ($argv.options.help == true)
	{
		delete $argv.options.help;
		help($argv);
		exit();
	}
	else if (_cmdHash[$argv.name])
	{
		_cmdHash[$argv.name].apply(_cmdHash[$argv.name], [...$argv.param, $argv.options])
		exit();
	}
}

module.exports = {
	has,
	checkAndStart,
	start
}